<?php
/**
 * @copyright Arthur Beer
 * @package core
 *
 * @copyright TinyWEB CMS
 * @link http://tinywebcms.ru
 * @link http://cms.vingrad.ru
 * @version SVN: $Id$ 
 */
 
/**
 * Location
 */
class Location 
{
    protected static $params = array();
    
    /**
     * Конструктор класса
     * 
     */
    private function __construct() {}
    
    public static function go($url)
    {
        //self::$params[] = array('back', Env::server('REQUEST_URI'));
	    header('Content-Type: text/html; charset=utf-8');
        header('location: ' . self::normalizeUrl($url));
        exit;
    }
    
    public static function go301($url)
    {
        header('Content-Type: text/html; charset=utf-8');
        header('HTTP/1.1 301 Moved Permanently');
        header('location: ' . self::normalizeUrl($url), null, 301);
        exit;
    }
    
    public static function normalizeUrl($url)
    {
        if(self::$params) {
            if(mb_strpos($url, '?') === false) {
                $url .= '?';
            } else if(mb_substr($url, -1) != '&' && mb_substr($url, -1) != '?') {
                $url .= '&';
            }
            
            foreach (self::$params as $param) {
                $url .= $param[0] . '=' . rawurlencode($param[1]) . '&';
            }
        }
        
        return rtrim($url, '&');
    }
    
    public static function addParam($param, $value)
    {
        array_push(self::$params, array($param, $value));
    }
    
    public static function reload($url = null)
    {
        self::go($url ? $url : self::selfUrl());
    }
    
    public static function selfUrl()
    {
        return Env::server('HTTP_X_URI', Env::server('REQUEST_URI', '/'));
    }
}

/**
 * Tab: 4 spaces
 * Encoding: utf-8
 */
?>